package com.jz.community.modulemine.information.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.jz.community.basecomm.base.BaseMvpActivity;
import com.jz.community.basecomm.bean.LoginBaseInfo;
import com.jz.community.basecomm.bean.UserInfo;
import com.jz.community.basecomm.mvp.BaseMvpPresenter;
import com.jz.community.basecomm.task.ITaskCallbackListener;
import com.jz.community.basecomm.utils.BaseSpUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.WpToast;
import com.jz.community.modulemine.R;
import com.jz.community.modulemine.R2;
import com.jz.community.modulemine.information.task.UpdateUserInfoTask;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Admin on 2018/3/30 0030.
 */

public class UpdateUserActivity extends BaseMvpActivity {

    @BindView(R2.id.title_back)
    ImageButton titleBack;
    @BindView(R2.id.edit_feedback)
    EditText editFeedback;
    @BindView(R2.id.title_operator)
    TextView titleOperator;
    @BindView(R2.id.title_name)
    TextView titleName;
    @BindView(R2.id.title_toolbar)
    Toolbar titleToolbar;

    @Override
    protected int getContentView() {
        return R.layout.module_mine_activity_note;
    }

    @Override
    protected void initView() {
        setImmersionBar(titleToolbar);
        titleOperator.setText("完成");
        titleName.setText("修改昵称");
        LoginBaseInfo loginBaseInfo = BaseSpUtils.getInstance().getUserBaseInfo(this);
        if (!Preconditions.isNullOrEmpty(loginBaseInfo)) {
            editFeedback.setText(loginBaseInfo.getName());
            if (!Preconditions.isNullOrEmpty(loginBaseInfo.getName())) {
                editFeedback.setSelection(loginBaseInfo.getName().length());
            }
        }
    }

    @Override
    protected void addListener() {
        titleBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        titleOperator.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                updateUserInfo(editFeedback.getText().toString().trim());
            }
        });
    }

    private void updateUserInfo(String nickName) {
        new UpdateUserInfoTask(this, new ITaskCallbackListener() {
            @Override
            public void doTaskComplete(Object o) {
                UserInfo userInfo = (UserInfo) o;
                if (!Preconditions.isNullOrEmpty(userInfo.getName())) {
                    WpToast.l(UpdateUserActivity.this, "修改成功");
                    Intent intent = new Intent();
                    intent.putExtra("nickName", userInfo.getName());
                    setResult(RESULT_OK, intent);
                    finish();
                } else {
                    WpToast.l(UpdateUserActivity.this, "修改失败");
                }

            }
        },true).execute(nickName, "1");
    }

    @Override
    protected BaseMvpPresenter createPresenter() {
        return null;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
